package com.zeny.edu.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeny.edu.common.ResponseResult;
import com.zeny.edu.eduservice.entity.EduCourse;
import com.zeny.edu.eduservice.entity.dto.CourseAllInfoDto;
import com.zeny.edu.eduservice.entity.dto.CourseInfoForm;
import com.zeny.edu.eduservice.entity.query.CourseQuery;
import com.zeny.edu.eduservice.service.EduCourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zeny
 * @since 2020-03-02
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * @param courseInfoForm
     * @return com.zeny.edu.common.ResponseResult
     * @Description 保存课程信息
     * @Date 2020/3/3 0003 14:45
     **/
    @PostMapping("/saveCourceInfo")
    public ResponseResult saveCourceInfo(@RequestBody CourseInfoForm courseInfoForm) {
        if (StringUtils.isNotBlank(courseInfoForm.getId())) {
            String courseId = eduCourseService.saveCourceInfo(courseInfoForm);
            return courseId != null ? ResponseResult.ok().data("courseId", courseId).message("课程基本信息修改成功") : ResponseResult.fail().message("课程信息修改失败");
        }
        String courseId = eduCourseService.saveCourceInfo(courseInfoForm);
        return courseId != null ? ResponseResult.ok().data("courseId", courseId).message("课程基本信息添加成功") : ResponseResult.fail().message("课程信息保存失败");
    }

    /**
     * @param id
     * @return com.zeny.edu.common.ResponseResult
     * @Description 根据ID查找课程信息
     * @Date 2020/3/3 0003 16:44
     **/
    @GetMapping("/getCourseInfoById/{id}")
    public ResponseResult getCourseInfoById(@PathVariable("id") String id) {
        CourseInfoForm courseInfoForm = eduCourseService.selectCourseInfoById(id);
        return ResponseResult.ok().data("courseInfo", courseInfoForm);
    }

    /**
     * @Description 分页查询课程列表
     * @Date 2020/3/4 0004 20:46
     * @param page        页码
     * @param limit       每页大小
     * @param courseQuery 条件封装
     * @return com.zeny.edu.common.ResponseResult
     **/
    @PostMapping("/pageList/{page}/{limit}")
    public ResponseResult pageQuery(@PathVariable Integer page,
                                    @PathVariable Integer limit,
                                    @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> coursePage = new Page<>(page, limit);
        IPage<EduCourse> courseList = eduCourseService.pageQuery(coursePage, courseQuery);
        return ResponseResult.ok().data("items", courseList.getRecords()).data("total", courseList.getTotal());
    }

    /**
     * @Description 根据ID删除课程
     * @Date 2020/3/5 0005 12:44
     * @param id
     * @return com.zeny.edu.common.ResponseResult
     **/
    @DeleteMapping("/deleteCourse/{id}")
    public ResponseResult deleteById(@PathVariable String id) {
        boolean success = eduCourseService.deleteById(id);
        return success ? ResponseResult.ok().message("删除成功") : ResponseResult.fail().message("删除失败");
    }

    /**
     * @Description 根据课程ID查询课程全部信息
     * @Date 2020/3/6 0006 0:56
     * @param courseId
     * @return com.zeny.edu.common.ResponseResult
     **/
    @GetMapping("/getAllCourseInfo/{courseId}")
    public ResponseResult getAllCourseInfo(@PathVariable String courseId) {
        CourseAllInfoDto courseAllInfoDto = eduCourseService.getAllCourseInfoById(courseId);
        return ResponseResult.ok().data("courseInfo", courseAllInfoDto);
    }


    /**
     * @Description 发布课程
     * @Date 2020/3/6 0006 11:46
     * @param id
     * @return com.zeny.edu.common.ResponseResult
     **/
    @GetMapping("/publshCourse/{id}")
    public ResponseResult publshCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean success = eduCourseService.updateById(eduCourse);
        return success ? ResponseResult.ok() : ResponseResult.fail();
    }

}

